package com.huatai.vba.common.utils;

import java.io.*;
import java.util.Enumeration;

import de.schlichtherle.util.zip.*;

public class ZipUtil {

	public static final String charset = "gbk";

	/**
	 * 压缩文件
	 * 
	 * @param zos
	 * @param file
	 * @throws Exception
	 */
	private static void fileZip(ZipOutputStream zos, File file) throws Exception {
		FileInputStream fis = null;
		try {
			if (file.isFile()) {
				zos.putNextEntry(new ZipEntry(file.getName()));
				fis = new FileInputStream(file);
				byte[] bb = new byte[2048];
				int aa = 0;
				while ((aa = fis.read(bb)) != -1) {
					zos.write(bb, 0, aa);
				}
			} else {
				directoryZip(zos, file, "");
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (fis != null)
				fis.close();
		}
	}

	/**
	 * 压缩一个文件夹
	 * 
	 * @param out
	 * @param f
	 * @param base
	 * @throws Exception
	 */
	private static void directoryZip(ZipOutputStream out, File f, String base) throws Exception {
		FileInputStream in = null;
		try {
			// 如果传入的是目录
			if (f.isDirectory()) {
				File[] fl = f.listFiles();
				// 创建压缩的子目录
				out.putNextEntry(new ZipEntry(base + "/"));
				if (base.length() == 0) {
					base = "";
				} else {
					base = base + "/";
				}
				if (fl != null) {
					for (File file : fl) {
						directoryZip(out, file, base + file.getName());
					}
				}
			} else {
				// 把压缩文件加入rar中
				out.putNextEntry(new ZipEntry(base));
				in = new FileInputStream(f);
				byte[] bb = new byte[2048];
				int aa = 0;
				while ((aa = in.read(bb)) != -1) {
					out.write(bb, 0, aa);
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (in != null)
				in.close();
		}

	}

	/**
	 * 对directory目录下的文件压缩，保存为指定的文件zipFile
	 * 
	 * @param directory
	 *            指定将被压缩的目录
	 * @param zipFile
	 *            指定压缩后的ZIP文件路径
	 */
	public static void zip(String zipFile, String directory) throws Exception {
		ZipOutputStream zos = null;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(zipFile);
			zos = new ZipOutputStream(fos, charset);
			fileZip(zos, new File(directory));
		} catch (Exception e) {
			throw e;
		} finally {
			if (zos != null)
				zos.close();
			if (fos != null)
				fos.close();
		}
	}

	// public static void main(String[] args) throws Exception {
	// String zipFile = "c:/db.rar";
	// String directory = "c:/dbconfig.ini";
	// String directory1 = "c:/hk_zqbdjs946.dbf";
	// String[] directorys = new String[] { "c:/dbconfig.ini",
	// "c:/hk_zqbdjs946.dbf" };
	// zip(zipFile, directorys);
	// }

	public static void zip(String zipFile, String[] directorys) throws Exception {
		ZipOutputStream zos = null;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(zipFile);
			zos = new ZipOutputStream(fos, charset);
			for (String directory : directorys) {
				fileZip(zos, new File(directory));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (zos != null)
				zos.close();
			if (fos != null)
				fos.close();
		}
	}

	/**
	 * 解压缩文件zipFile保存在directory目录下
	 * 
	 * @param directory
	 *            指定文件释放的目录
	 * @param filePath
	 *            将被解压的ZIP文件路径
	 * @throws Exception
	 */
	public static void unZip(String filePath, String directory) throws Exception {
		InputStream fiss = null;
		FileOutputStream fos = null;
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(filePath, charset);
			Enumeration e = zipFile.entries();
			while (e.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) e.nextElement();
				String entryName = zipEntry.getName();
				fiss = zipFile.getInputStream(zipEntry);
				File f = new File(directory + entryName);
				if (zipEntry.isDirectory()) {
					f.mkdirs();
				} else {
					f.createNewFile();
					fos = new FileOutputStream(f);
					byte b[] = new byte[2048];
					int aa = 0;
					while ((aa = fiss.read(b)) != -1) {
						fos.write(b, 0, aa);
					}
				}
			}
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
				if (fiss != null) {
					fiss.close();
				}
				if (zipFile != null) {
					zipFile.close();
				}
			} catch (Exception e) {
				throw e;
			}
		}
	}
}
